﻿Friend Class FViewNetworkPackage
    Public Shared Shadows Sub Show(ByVal owner As System.Windows.Forms.IWin32Window, ByVal package As Framework.Net.Manager.NetworkDataPackage)
        Dim clsView As New FViewNetworkPackage
        clsView.ShowIt(owner, package)
    End Sub

    Private Sub ShowIt(ByVal owner As System.Windows.Forms.IWin32Window, ByVal package As Framework.Net.Manager.NetworkDataPackage)
        If package.TextData IsNot Nothing Then
            Dim strText As String = package.TextData.Replace("<?xml version=""1.0"" encoding=""utf-16""?>", String.Empty)
            strText = strText.Replace(" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""", String.Empty)
            If strText.StartsWith(vbNewLine) Then strText = strText.Substring(vbNewLine.Length)
            TextBox1.Text = strText
        Else
            Dim clsSB As New System.Text.StringBuilder
            Dim j As Integer = 0
            For i As Integer = 0 To package.RawData.Length - 1 Step 16
                AddDataBlockLine(clsSB, package.RawData, i, 16)
                j = i
            Next
            If j + 16 < package.RawData.Count Then
                AddDataBlockLine(clsSB, package.RawData, j + 16, package.RawData.Count - j - 16)
            End If
            TextBox1.Text = clsSB.ToString
        End If
        MyBase.Show(owner)
    End Sub
    Private Sub AddDataBlockLine(ByVal sb As System.Text.StringBuilder, ByVal buffer() As Byte, ByVal start As Integer, ByVal maxCount As Integer)
        sb.Append(String.Format("{0:X8}", start))
        sb.Append(" "c)
        For i As Integer = start To start + maxCount
            If i < buffer.Count Then
                sb.Append(String.Format("{0:X2}", buffer(i)))
                sb.Append(" "c)
            Else
                sb.Append("   ")
            End If
        Next
        For i As Integer = start To start + maxCount
            If i < buffer.Count Then
                If buffer(i) < 32 Then
                    sb.Append("."c)
                Else
                    sb.Append(System.Text.Encoding.UTF8.GetString(buffer, i, 1))
                End If
            Else
                sb.Append(" "c)
            End If
        Next
        sb.AppendLine()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class